package com.macro.mall.portal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.dao.QrImageMapper;
import com.macro.mall.portal.domain.BillLading;
import com.macro.mall.portal.domain.QrImage;
import com.macro.mall.portal.domain.TxCos;
import com.macro.mall.portal.domain.vo.PageVo;
import com.macro.mall.portal.service.QrImageService;
import com.macro.mall.portal.service.TxCosService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.util.List;

/**
 * 微信公众号用户二维码 前端控制器
 *
 * @author db
 * @since 2021-08-21
 */
@RestController
@RequestMapping("/qrImage")
@Api(tags = "QrImageController", description = "公众号用户二维码")
public class QrImageController {

    @Autowired
    private QrImageService qrImageService;

    @Autowired
    private QrImageMapper qrImageMapper;

    @Autowired
    private TxCosService txCosService;

    @ApiOperation("添加信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody QrImage qrImage) {
        try {
            QueryWrapper<QrImage> wrapper = new QueryWrapper<>();
            wrapper.eq("open_id", qrImage.getOpenId());
            qrImageService.remove(wrapper);

            qrImageService.save(qrImage);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("添加成功");
    }

    @ApiOperation("修改信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody QrImage qrImage) {
        try {
            qrImageService.updateById(qrImage);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("查询信息")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public CommonResult query(@RequestBody QrImage qrImage) {
        QrImage qr = null;
        try {
            QueryWrapper<QrImage> wrapper = new QueryWrapper<>();
            wrapper.eq("open_id", qrImage.getOpenId());
            qr = qrImageMapper.selectOne(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        if (StringUtils.isEmpty(qr)) {
            return CommonResult.failed("没有查询到");
        }
        return CommonResult.success(qr);
    }

    @ApiOperation("查询cos")
    @RequestMapping(value = "/queryCos", method = RequestMethod.POST)
    public CommonResult queryCos() {
        List<TxCos> list = null;
        try {
            list = txCosService.list();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(list);
    }

}
